﻿#define _CRT_SECURE_NO_WARNINGS  1
//一定要动手，例子不管多么简单，建议至少自己手敲一遍看看是否理解了里头的细枝末节。
//一定要学会思考，思考为什么要这样，而不是那样。还要举一反三地思考
#include"workManager.h"
//构造函数
WorkManager::WorkManager()
{
	ifstream ifs;
	ifs.open(FILENAME, ios::in);//以读方式打开
	if (!ifs.is_open())
	{
		//第一种情况文件不存在时
		//文件不存在
		/*cout << "文件不存在" << endl;*/
		//初始化员工人数
		this->_EmpNum = 0;
		//初始化数组指针
		this->_EmpArray = NULL;
		//初始化探空数组
		this->FileIsEmpty = true;
		ifs.close();//关闭文件
		return;
	}
	//文件存在，但文件为空
	char p;
	ifs >> p;
	if (ifs.eof())//ifs.eof()  当文件为空时返回真，不为空时返回假
	{
		//初始化员工人数
		this->_EmpNum = 0;
		//初始化数组指针
		this->_EmpArray = NULL;
		//初始化探空数组
		this->FileIsEmpty = true;
		ifs.close();//关闭文件
		return;
	}
	//如果文件存在且不为空
	int n = Get_Numer();
	//cout << "职工人数为：" << n << endl;
	this->_EmpNum = n;//更新人数
	//开辟空间
	this->_EmpArray = new work* [this->_EmpNum];
	//数据初始化
	Init_Emp();
}

//展示菜单
void WorkManager::Show_menu()
{
	cout << "********************************************" << endl;
	cout << "*********** 欢迎使用职工管理系统 ***********" << endl;
	cout << "************** 0.退出程序 ******************" << endl;
	cout << "************** 1.增加员工信息 **************" << endl;
	cout << "************** 2.显示员工信息 **************" << endl;
	cout << "************** 3.删除员工信息 **************" << endl;
	cout << "************** 4.修改员工信息 **************" << endl;
	cout << "************** 5.查找员工信息 **************" << endl;
	cout << "************** 6.按照编号排序 **************" << endl;
	cout << "************** 7.清空所有文档 **************" << endl;
	cout << endl;
}
//退出程序
void  WorkManager::ExitSystem()
{
	cout << "欢迎下次使用" << endl;
	system("pause");
	exit(0);
}
//添加人数
void WorkManager::AddEmp()
{
	cout << "请输入要添加的人数->;" << endl;
	int _num = 0;//记录添加的人数
	cin >> _num;
	if (_num > 0)
	{
		//添加
		//确定总人数
		int size = this->_EmpNum + _num;
		//开辟空间
		work** newspace = new work * [size+1];
		//判断原数组是否为空
		if (this->_EmpArray != nullptr)
		{
			//拷贝原数据
			for (int i = 0; i < this->_EmpNum; i++)
			{
				newspace[i] = this->_EmpArray[i];
			}
		}
		//添加新数据
		for (int i = 0; i < _num; i++)
		{
			int id = 0;
			string _name;
			int did = 0;
			mark:
			cout << "请输入第" << i + 1 << "个新员工的编号：" << endl;
			cin >> id;
			int ret = Is_Exit(id);
			if (ret !=-1 )
			{
				cout << "错误，该编号已有，请重新输入" << endl;
				goto mark;
			}
			cout << "请输入第" << i + 1 << "个新员工的姓名：" << endl;
			cin >> _name;
			cout << "请选择该职工的岗位：" << endl;
			cout << "1. 普通员工" << endl;
			cout << "2. 经理" << endl;
			cout << "3. 总裁" << endl;
			cin >> did;

			work* worker = NULL;
			switch (did)
			{
			case 1:
				worker = new employee(id, _name, 1);
				break;
			case 2:
				worker = new manager(id, _name, 2);
				break;
			case 3:
				worker = new boss(id, _name, 3);
				break;
			default:
				break;
			}
			//将创建职工职责，保存在数组中
			newspace[this->_EmpNum + i] = worker;
		}
		//释放原有空间
		delete[] this->_EmpArray;
		//更改新空间
		this->_EmpArray = newspace;
		//更新新的职工人数
		this->_EmpNum = size;
		//提示成功添加
		cout << "成功添加了" << _num << "名新员工" << endl;
		//更新职工不为空标志
		
		this->FileIsEmpty = false;
		//保存数据
		save();
	}

	else {
		cout << "输入数据错误" << endl;
	}

	system("pause");
	system("cls");
}
//保存数据
void WorkManager::save()
{
	//创建文件流对象
	ofstream ofs;
	//打开文件
	ofs.open(FILENAME, ios::out);
	//写入文件
	for (int i = 0; i < this->_EmpNum; i++)
	{
		ofs << this->_EmpArray[i]->_Id << " "
			<< this->_EmpArray[i]->_name << " "
			<< this->_EmpArray[i]->_DeptId << endl;
	 }
	//关闭文件
	ofs.close();
}

int WorkManager::Get_Numer()
{
	ifstream ifs;
	ifs.open(FILENAME, ios::in);//以读的方式打开
	if (!ifs.is_open())
	{
		ifs.close();
		return 0;
	}
	int id;
	string _name;
	int dId;
	int num = 0;
	while (ifs >> id && ifs >> _name && ifs >> dId)
	{
		num++;
	}
	
	return num;

}
void  WorkManager::Show_Emp()
{
	if (this->FileIsEmpty)
	{
		cout << "文件为空或文件不存在" << endl;
	}
	else
	{
	/*	cout << "员工人数为： " << this->_EmpNum << endl;*/
		//显示员工信息
		for (int i = 0; i < this->_EmpNum; i++)
		{
			//调用多态调用程序接口
			this->_EmpArray[i]->ShowInf();
		}
	}
	//按任意键退出
	system("pause");
	system("cls");
}
//初始化员工
void WorkManager::Init_Emp()
{
	ifstream ifs;
	ifs.open(FILENAME, ios::in);
	int id;
	string name;
	int did;
	int _indix = 0;
	while (ifs >> id && ifs >> name && ifs >> did)
	{
		work* worker = NULL;
		if (did == 1)//普通员工
		{
			worker = new employee(id, name, did);
		}
		if (did == 2)//经理
		{
			worker = new manager(id, name, did);
		}
		if (did == 3)//老板
		{
			worker = new boss(id, name, did);
		}
		this->_EmpArray[_indix] = worker;
		_indix++;
	}
	ifs.close();
}

int  WorkManager::Is_Exit(int id )
{
	for (int i = 0; i < this->_EmpNum; i++)
	{
		if (this->_EmpArray[i]->_Id == id)
		{
			return i;
		}
	}
	return -1;
}
//删除员工
void WorkManager::Del_Emp()
{
	if (this->FileIsEmpty)
	{
		cout << "文件不存在或记录为空！" << endl;
	}
	else {
		//按照员工编号删除
		cout << "请输入要删除的员工编号->:" << endl;
		int input = 0;
		cin >> input;
		int ret = Is_Exit(input);
		if (ret == -1)//说明员工不存在
		{
			cout << "删除失败，未找到该员工" << endl;

		}
		else {//员工存在，并且要删除ret位置上的员工
			int end = ret + 1;
			while (end < this->_EmpNum)
			{
				this->_EmpArray[end - 1] = this->_EmpArray[end];
				end++;
			}
			this->_EmpNum--;
			//数据同步更新
			this->save();
			cout << "数据删除成功!" << endl;
		}
	}
	system("pause");
	system("cls");
}
//修改职工
void WorkManager::Mod_Emp()
{
	if (this->FileIsEmpty)
	{
		cout << "文件不存在或记录为空！" << endl;
	}
	else
	{
		cout << "请输入要修改员工的编号->:" << endl;
		int index = 0;
		cin >> index;
		int ret = Is_Exit(index);
		if (ret == -1)
		{
			cout << "修改失败，查无此人" << endl;
		}
		else {
			//修改信息
			delete this->_EmpArray[ret];
			//cout << "标记1" << endl;
			int id = 0;
			string _name;
			int did = 0;
			cout << "找到编号为"<< index<<"的员工，请输入新员工的编号：" << endl;
			cin >> id;
			cout << "请输入新员工的姓名：" << endl;
			cin >> _name;
			cout << "请输入新员工的岗位：" << endl;
			cout << "1. 普通员工" << endl;
			cout << "2. 经理" << endl;
			cout << "3. 总裁" << endl;
			cin >> did;

			work* worker = NULL;
			switch (did)
			{
			case 1:
				worker = new employee(id, _name, 1);
				break;
			case 2:
				worker = new manager(id, _name, 2);
				break;
			case 3:
				worker = new boss(id, _name, 3);
				break;
			default:
				break;
			}
			//记录数据到数组中
			this->_EmpArray[ret] = worker;
			cout << "修改成功" << endl;
			//将数据更新到文件中
			this->save();
		}
	}
	//按任意键清屏
	system("pause");
	system("cls");
}
void WorkManager::Find_Emp()
{
	//选择按什么查找
	int input = -1;
	cout << "选择查找方式 1.编号 2.姓名 请输入 1 或 2  ->:" <<endl;
	cin >> input;
	if (input == 1)
	{
		//按编号查找
		cout << "请输入查找职员的编号:>" << endl;
		int index = 0;
		cin >> index;
		int ret = Is_Exit(index);
		if (ret == -1)//说明员工不存在
		{
			cout << "删除失败，未找到该员工" << endl;
		}
		else {
			cout << "查找成功，信息如下：" << endl;
			//调用显示信息接口：
			this->_EmpArray[ret]->ShowInf();
		}
	}
	else if (input == 2)
	{
		cout << "请输入要查找员工的姓名：->" << endl;
		string name = "";
		cin >> name;
		int flage = 0;
		//按姓名查找
		for (int i = 0; i < this->_EmpNum; i++)
		{
			if (this->_EmpArray[i]->_name == name)
			{
				flage = 1;
				cout << "找到该员工了，信息如下：" << endl;
				//调用显示信息接口：
				this->_EmpArray[i]->ShowInf();
			}
		}
		if (flage == 0)
		{
			cout << "未找到该员工！" << endl;
		}
	}
	else {
		cout << "输入错误，请重新输入" << endl;
	}
	system("pause");
	system("cls");
}
//交换函数
void swap(work** x, work** y)
{
	work* tmp = NULL;
	tmp = *x;
	*x = *y;
	*y = tmp;
}
//排序员工
void WorkManager::SelectEmp()
{
	if (this->FileIsEmpty)
	{
		cout << "文件不存在或记录为空！" << endl;
		system("pause");
		system("cls");
	}
	else {
		cout << "1.升序 2.降序 请选择1/2 ->: " << endl;
		int input = 0;
		cin >> input;
			//排降序
			//排升序  选择排序来实现 思想：遍历数据选出最大值和最小值，与下标为left 和 right 交换
			for (int i = 0; i < this->_EmpNum; i++)
			{
				int minOrMax = i;//声明最小值或最大值的下标
				for (int j = i + 1; j < this->_EmpNum; j++)
				{
					if (input == 1)
					{
						//排升序  选择排序来实现 思想：遍历数据选出最大值和最小值，与下标为begin 和end 交换
						if (this->_EmpArray[minOrMax]->_Id > this->_EmpArray[j]->_Id)
						{
							minOrMax = j;
						}
					}
					else//降序
					{
						if (this->_EmpArray[minOrMax]->_Id < this->_EmpArray[j]->_Id)
						{
							minOrMax = j;
						}
					}
					
				}//判断一开始认定的最下值或最大值是不是计算的最小值或最大值  如果不是交换
				if (i != minOrMax)
				{
					work* temp = this->_EmpArray[i];
					this->_EmpArray[i] = this->_EmpArray[minOrMax];
					this->_EmpArray[minOrMax] = temp;
				}
			}
			cout << "排序成功！排序后的结果为：" << endl;
			this->save();//保存到文件中
			this->Show_Emp();//展示所有的职工
	}

}
//清空文件
void WorkManager::Clean_File()
{
	cout << "确定清空文件吗？ 确定 1  返回 2 " << endl;
	int select = 0;
	cin >> select;
	if (select == 1)
	{
		//清空文件
		ofstream ofs(FILENAME, ios::trunc);//删除文件后重新创建
		ofs.close();
		if (this->_EmpArray != nullptr)
		{
			//清空指针数组中的元素
			for (int i = 0; i < this->_EmpNum; i++)
			{
				delete this->_EmpArray[i];
				this->_EmpArray[i] = NULL;
			}
			delete[] this->_EmpArray;
			this->_EmpArray = NULL;
			this->_EmpNum = 0;
			this->FileIsEmpty = true;
		}
		cout << "清空成功" << endl;
	}
	system("pause");
	system("cls");
}
//析构函数
WorkManager:: ~WorkManager()
{
	if (this->_EmpArray != NULL)
	{
		for (int i = 0; i < this->_EmpNum; i++)
		{
			if (this->_EmpArray[i] != nullptr)
			{
				delete this->_EmpArray[i];
			}
		}
		delete[] this->_EmpArray;
		this->_EmpArray = NULL;
	}
}
